﻿using System;
using System.Reflection;
using DevExpress.XtraEditors;
using NetROS.Tiers.Entities;

namespace NetRosApp.Klijenti.Services
{
    static class TraziUtility
    {


        public static Type GetViewForDokumentTip(int IDDokumentTip)
        {
            Type EditorClass = null;
            switch (IDDokumentTip)
            {
                case (int)DokumentTipList.Međuskladišnica_Izdana_Robno:
                    EditorClass = typeof(MeduskladisnicaView);
                    break;
                case (int)DokumentTipList.Međuskladišnica_Izdana_Storno:
                    EditorClass = typeof(MeduskladisnicaView);
                    break;
                case (int)DokumentTipList.Međuskladišnica_Primljena_Robno:
                    EditorClass = typeof(MedjuskladisnicaPrijemniView);
                    break;
                case (int)DokumentTipList.Međuskladišnica_prijem_Storno:
                    EditorClass = typeof(MedjuskladisnicaPrijemniView);
                    break;
                case (int)DokumentTipList.Otkaz_rezervacije:
                    EditorClass = typeof(RezervacijaViewVer2);
                    break;
                case (int)DokumentTipList.Otpremnica:
                    EditorClass = typeof(RacunView);
                    break;
                case (int)DokumentTipList.Otpremnica_Storno:
                    EditorClass = typeof(RacunView);
                    break;
                case (int)DokumentTipList.Povrat_reversa:
                    EditorClass = typeof(ReversViewVer2);
                    break;
                case (int)DokumentTipList.Početno_stanje:
                    break;
                case (int)DokumentTipList.Početno_stanje_storno:
                    break;
                case (int)DokumentTipList.Prijemni_list:
                    EditorClass = typeof(PrimkaView);
                    break;
                case (int)DokumentTipList.Prijemni_list_Storno:
                    EditorClass = typeof(PrimkaView);
                    break;
                case (int)DokumentTipList.Revers:
                    EditorClass = typeof(ReversViewVer2);
                    break;
                case (int)DokumentTipList.Rezervacija:
                    EditorClass = typeof(RezervacijaViewVer2);
                    break;
                case (int)DokumentTipList.Zapisnik_o_promjeni_cijene:
                    EditorClass = typeof(PromjenaCijeneView);
                    break;
                case (int)DokumentTipList.Racun:
                    EditorClass = typeof(RacunAView);
                    break;
                case (int)DokumentTipList.Racun_storno:
                    EditorClass = typeof(RacunAView);
                    break;
                case (int)DokumentTipList.Racun_otpremnica:
                    EditorClass = typeof(RacunOtpremnicaView);
                    break;
                case (int)DokumentTipList.Racun_otpremnica_storno:
                    EditorClass = typeof(RacunOtpremnicaView);
                    break;
                case (int)DokumentTipList.Ponuda:
                    EditorClass = typeof(RacunPonudaView);
                    break;
                case (int)DokumentTipList.Ponuda_storno:
                    EditorClass = typeof(RacunPonudaView);
                    break;
                case (int)DokumentTipList.Predračun:
                    EditorClass = typeof(RacunPredRacunView);
                    break;
                case (int)DokumentTipList.Predračun_storno:
                    EditorClass = typeof(RacunPredRacunView);
                    break;
                case (int)DokumentTipList.Narudžba_storno:
                    EditorClass = typeof(NarudzbaView);
                    break;
                case (int)DokumentTipList.Narudžba:
                    EditorClass = typeof(NarudzbaView);
                    break;
                case (int)DokumentTipList.Racun_usluga_storno:
                    EditorClass = typeof(RacunUslugaView);
                    break;
                case (int)DokumentTipList.Racun_usluga:
                    EditorClass = typeof(RacunUslugaView);
                    break;
                case (int)DokumentTipList.Izdatnica_storno:
                    EditorClass = typeof(IzdatnicaView);
                    break;
                case (int)DokumentTipList.Izdatnica:
                    EditorClass = typeof(IzdatnicaView);
                    break;
                case (int)DokumentTipList.Racun_tranzitni_storno:
                    EditorClass = typeof(RacunTranzitniView);
                    break;
                case (int)DokumentTipList.Racun_tranzitni:
                    EditorClass = typeof(RacunTranzitniView);
                    break;
                case (int)DokumentTipList.Račun_MP:
                    EditorClass = typeof(RacunMPView);
                    break;
                case (int)DokumentTipList.Račun_MP_storno:
                    EditorClass = typeof(RacunMPView);
                    break;
                case (int)DokumentTipList.Račun_MP_Servis:
                    EditorClass = typeof(RacunMPServis);
                    break;
                case (int)DokumentTipList.Račun_MP_Serivs_storno:
                    EditorClass = typeof(RacunMPServis);
                    break;
                case 51:
                    EditorClass = typeof(RadniNalogView);
                    break;
                case 52:
                    EditorClass = typeof(RadniNalogView);
                    break;


                default:
                    break;

            }
            return EditorClass;
        }
        //}

        public static void AddBracketsAndShowInTextEdit(object p, TextEdit pTextEdit)
        {

            if (p != null)
            {
                string tmpstring = Convert.ToString(p);
                //if (tmpstring.Length != 0)
                //    pTextEdit.Text = "\"" + tmpstring + "*\"";
            }
        }
        public static void ShowInTextEdit(object p, TextEdit pTextEdit)
        {

            if (p != null)
            {
                string tmpstring = Convert.ToString(p);
                if (tmpstring.Length != 0)
                    pTextEdit.Text = tmpstring;
            }
        }

        public static string AddBrackets(string p)
        {
            string tmpstring = "";
            if (p != null)
            {
                tmpstring = Convert.ToString(p);
                //if (tmpstring.Length != 0)
                //    tmpstring = "\"" + tmpstring + "\"";
            }
            return tmpstring;
        }
        public static string AddBracketsStar(string p)
        {
            string tmpstring = "";
            if (p != null)
            {
                tmpstring = Convert.ToString(p);
                if (tmpstring.Length != 0)
                    tmpstring = "" + tmpstring + "*";
            }
            return tmpstring;
        }



        public static string GetParametersForEntity<EntityType, ParameterBuilderType>(object servis, object value, string NazivKolone)
        {
            if ((value as String) != null && ((string)value).Length == 0) return string.Empty;
            object ParameterBuidler = Activator.CreateInstance(typeof(ParameterBuilderType));

            String foundBankaResult = "\" \"";

            System.Collections.IEnumerable _foundBankaList;

            MethodInfo append = ParameterBuidler.GetType().GetMethod("Append", new Type[] { typeof(string), typeof(string) });

            object[] mParam = new object[] { NazivKolone, value };

            append.Invoke(ParameterBuidler, mParam);

            object foundparamet = ParameterBuidler.GetType().GetMethod("GetParameters").Invoke(ParameterBuidler, null);

            object[] mParamServis = new object[] { foundparamet };

            MethodInfo FindService = servis.GetType().GetMethod("Find", new Type[] { typeof(NetROS.Tiers.Data.SqlFilterParameterCollection) });

            _foundBankaList = FindService.Invoke(servis, mParamServis) as System.Collections.IEnumerable;

            if (_foundBankaList != null)
                foreach (object _foundBanka in _foundBankaList)
                {
                    if (_foundBanka.GetType() == typeof(EntityType))
                    {
                        foundBankaResult += ", " + AddBrackets(_foundBanka.GetType().GetProperty("ID").GetValue(_foundBanka, null).ToString());
                    }
                }
            return foundBankaResult;
        }



        static public object getobject(object p, string field)
        {
            PropertyInfo property = p.GetType().GetProperty(field);
            try
            {
                return property.GetValue(p, null);
            }

            catch
            {
                return null;
            }
        }

        static public object getUnboundValue(object p, string[] field)
        {
            foreach (string tmpstr in field)
            {
                if (p != null)
                {
                    p = getobject(p, tmpstr);
                }
                else
                {
                    return String.Empty;
                }

            }
            if (p != null) return p;

            return String.Empty;
        }

        static public object getUnboundValue(object p, string value)
        {
            string[] tmp = value.Split('.');
            return getUnboundValue(p, tmp);
        }

        static public void setUnboundValue(object p, string field, object p_3)
        {
            PropertyInfo property = p.GetType().GetProperty(field);
            property.SetValue(p, p_3, null);
        }

    }
}
